package com.yvan.codeslave.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.yvan.codeslave.PluginUtils;
import com.yvan.codeslave.snippet.Idea;
import com.yvan.codeslave.snippet.SnippetService;
import lombok.val;
import org.jetbrains.annotations.Nullable;

public class SnippetAction extends AnAction {

    private final String menuName;

    public SnippetAction(@Nullable String menuName) {
        super(menuName);
        this.menuName = menuName;
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        val template = SnippetService.getMapGroupTemplate().getTemplate(menuName);
        template.binding("tables", PluginUtils.getSelectStructs().getInnerMap());

        try {
            val v = template.render();
            Idea.notifyInfo(v);

        } catch (Exception ex) {
            Idea.rethrow(ex);
        }
    }
}
